package com.hujiheng.paike.controller;

import com.hujiheng.paike.common.lang.Result;
import com.hujiheng.paike.pojo.*;
import com.hujiheng.paike.service.*;
import com.hujiheng.paike.utils.CoursesUtils;
import com.hujiheng.paike.view.CoursesView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/course")
public class ClassScheduleController {
    @Autowired
    private ClassCourseService classCourseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private CourseService courseService;


    @RequestMapping("/class/add")
    public Result addClassCourse(@Validated @RequestBody ClassCourse classCourse) {
        ClassCourse classCourse1 = classCourseService.getClassCourseByDayAndSection(classCourse.getWhichDay(), classCourse.getWhichSection(), classCourse.getClassId());
        if (classCourse1 != null) {
            int update = classCourseService.update(classCourse);
            if (update < 1) {
                return Result.fail("更新失败");
            }
            return Result.success("更新成功");
        }
        int add = classCourseService.add(classCourse);
        if (add < 1) {
            return Result.fail("添加失败");
        }
        return Result.success("添加成功");
    }

    @RequestMapping("/teacher/{id}")
    public Result searchByTeacherId(@PathVariable("id") int teacherId) {
        //查询教师教的哪一节课
        List<ClassCourse> courses = classCourseService.getClassCourseByTeacherId(teacherId);
        if (courses.size() < 1) {
            return Result.fail("出错了");
        }
        //获取教师教的课程
        Teacher teacher = teacherService.getById(teacherId);
        Course courseById = courseService.getCourseById(teacher.getCourseId());
        //整合信息
        List<CoursesView> coursesViews = CoursesUtils.getCoursesViews();
        for (ClassCourse course : courses) {
            CoursesUtils.setCoursesViews(coursesViews, course.getWhichDay(), course.getWhichSection(), courseById.getCourseName());
        }
        return Result.success(coursesViews);
    }

    @RequestMapping("/student/{id}")
    public Result searchByStudentId(@PathVariable("id") int studentId) {
        Student student = studentService.getById(studentId);
        if (student == null) {
            return Result.fail("查询失败");
        }
        return searchByStudentIdOrClassId(student.getClassId());
    }

    @RequestMapping("/class/{id}")
    public Result searchByClassId(@PathVariable("id") int classId) {
//        List<ClassCourse> classCourseByClassId = classCourseService.getClassCourseByClassId(classId);
//        if (classCourseByClassId.size() < 1) {
//            return Result.fail("查询失败");
//        }
//        List<CoursesView> coursesViews = CoursesUtils.getCoursesViews();
//        for (ClassCourse course : classCourseByClassId) {
//            Teacher byId = teacherService.getById(course.getTeacherId());
//            Course courseById = courseService.getCourseById(byId.getCourseId());
//            CoursesUtils.setCoursesViews(coursesViews, course.getWhichDay(), course.getWhichSection(), courseById.getCourseName());
//        }
        return searchByStudentIdOrClassId(classId);
//
//        return Result.success(coursesViews);
    }

    private Result searchByStudentIdOrClassId(int classId) {
        ClassInfo classInfo = classInfoService.getClassInfoById(classId);
        if (classInfo == null) {
            return Result.fail("查询失败");
        }
        List<ClassCourse> classCourseByClassId = classCourseService.getClassCourseByClassId(classInfo.getClassId());
        if (classCourseByClassId.size() < 1) {
            return Result.fail("查询失败");
        }
        List<CoursesView> coursesViews = CoursesUtils.getCoursesViews();
        for (ClassCourse course : classCourseByClassId) {
            Teacher byId = teacherService.getById(course.getTeacherId());
            Course courseById = courseService.getCourseById(byId.getCourseId());
            CoursesUtils.setCoursesViews(coursesViews, course.getWhichDay(), course.getWhichSection(), courseById.getCourseName());
        }
        return Result.success(coursesViews);
    }
}
